package com.changgou.pay.controller;

import com.changgou.orders.feign.OrderFeign;
import com.changgou.orders.pojo.Order;
import com.changgou.pay.service.WeiXinPayService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping(value = "/weixin/pay")
@CrossOrigin
public class WeiXinPayController {

    @Autowired
    private WeiXinPayService weixinPayService;

    @Autowired
    private OrderFeign orderFeign;

    /***
     * 创建二维码
     * @return
     */
    @RequestMapping(value = "/create/native")
    public Result createNative(String outtradeno, String money) {
        Map<String, String> resultMap = weixinPayService.createNative(outtradeno, money);
        return new Result(true, StatusCode.OK, "创建二维码预付订单成功！", resultMap);
    }

    @GetMapping(value = "/query")
    public Result queryOder(String outTradNo) {
        Map<String, String> resp = weixinPayService.queryPayStatus(outTradNo);
        //如果支付成功则更新订单状态
        if (resp.get("result_code").equals("SUCCESS")) {
            Order order = new Order();
            String orderId = outTradNo.replace("No", "");
            order.setId(orderId);
            order.setPayStatus("支付成功");
            orderFeign.update(order, orderId);

        }
        return new Result(true, StatusCode.OK, "查询状态成功！", resp);
    }
}